#include "List.cpp"

//存在一个按升序排列的链表，给你这个链表的头节点 head ，请你删除链表中所有存在数字重复情况的节点，只保留原始链表中没有重复出现的数字。
//
//返回同样按升序排列的结果链表。


ListNode* deleteDuplicates(ListNode* head) {
    ListNode* newHead = (ListNode*) malloc(sizeof(ListNode));
    newHead->next = NULL;
    ListNode* pos = newHead;
    if (head == NULL) {
        return NULL;
    }

    ListNode* p = head;
    while (p != NULL) {
        if (p->next != NULL && p->data == p->next->data) {
            int pre = p->data;
            while (p != NULL && p->data == pre) {
                p = p->next;
            }
            continue;
        }
        ListNode* node = (ListNode*) malloc(sizeof(ListNode));
        node->next = NULL;
        node->data = p->data;

        pos->next = node;
        pos = pos->next;

        p = p->next;
    }
    return newHead->next;
}












ListNode* deleteDuplicates1(ListNode* head) {
    ListNode* newHead = (ListNode*) malloc(sizeof(ListNode));
    newHead->next = NULL;
    ListNode* pos = newHead;
    if (head == NULL) {
        return NULL;
    }


    ListNode *q = head;

    ListNode *p = q;
    int count = 0;

    while (p != NULL) {
        if (q != NULL && p->data == q->data) {
            count++;
            q = q->next;
            continue;
        }
        if (count == 1) {
            ListNode* node = (ListNode*) malloc(sizeof(ListNode));
            node->next = NULL;
            node->data = p->data;

            pos->next = node;
            pos = pos->next;
        }
        count = 0;
        p = q;
    }

//    while (q != NULL) {
//        if (p->data == q->data) {
//            count++;
//            q = q->next;
//            continue;
//        }
//        if (count == 0) {
//            ListNode* node = (ListNode*) malloc(sizeof(ListNode));
//            node->next = NULL;
//            node->data = p->data;
//
//            pos->next = node;
//            pos = pos->next;
//        }
//        count = 0;
//        p = q;
//        q = q->next;
//    }
//
//    if (p->next == q) {
//        ListNode* node = (ListNode*) malloc(sizeof(ListNode));
//        node->next = NULL;
//        node->data = p->data;
//
//        pos->next = node;
//        pos = pos->next;
//    }



    return newHead->next;
}


int main() {
    int arr[] = {1,1,2,3,3,4,5,5};
    ListNode* head = CreateListByArray(arr, ARRAY_SIZE(arr));
    PrintList(deleteDuplicates1(head));
}






















